const Router = require('koa-router');
const router = new Router();

/* 
var Q = require('q');
var promise = require('promise');

const ccc = () => {
    "use strict";

};

function mysqlQuery() {
    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'admin123',
        database: 'test20180217'
    });

    var deferred = Q.defer();

    connection.on('error', function () {
        console.error('createConnection 请正确启动 mysql 服务器');
        return deferred.reject('createConnection 请正确启动 mysql 服务器');
    });

    connection.connect(function (err) {
        if (err) {
            console.error('error connecting: ' + err.stack);
            return deferred.reject('error connecting: ' + err.stack);
        }
        console.log('connected as id ' + connection.threadId);
    });

    var sql = 'SELECT * FROM websites';
    connection.query(sql, function (error, results, fields) {
        if (error) {
            console.log('[SELECT ERROR] - ', error.message);

            connection.end();
            return deferred.reject('[SELECT ERROR] - ', error.message);
        }

        console.dir(results);
        deferred.resolve(results);
        connection.end();
    });

    return deferred.promise;
}

function mysqlDelete() {
    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'admin123',
        database: 'test20180217'
    });

    var deferred = Q.defer();

    connection.on('error', function () {
        console.error('createConnection 请正确启动 mysql 服务器');
        return deferred.reject('createConnection 请正确启动 mysql 服务器');
    });

    connection.connect(function (err) {
        if (err) {
            console.error('error connecting: ' + err.stack);
            return deferred.reject('error connecting: ' + err.stack);
        }
        console.log('connected as id ' + connection.threadId);
    });

    var delSql = 'DELETE FROM websites where id=6';

    connection.query(delSql, function (error, results, fields) {
        if (error) {
            console.log('[DELETE ERROR] - ', error.message);
            connection.end();
            return deferred.reject('[DELETE ERROR] - ', error.message);
        }

        console.log('DELETE affectedRows', results.affectedRows);
        deferred.resolve('DELETE affectedRows', results.affectedRowss);
        connection.end();
    });

    return deferred.promise;
}

function mysqlInsert() {
    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'admin123',
        database: 'test20180217'
    });

    var deferred = Q.defer();

    connection.on('error', function () {
        console.error('createConnection 请正确启动 mysql 服务器');
        return deferred.reject('createConnection 请正确启动 mysql 服务器');
    });

    connection.connect(function (err) {
        if (err) {
            console.error('error connecting: ' + err.stack);
            return deferred.reject('error connecting: ' + err.stack);
        }
        console.log('connected as id ' + connection.threadId);
    });

    var addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)';
    var addSqlParams = ['菜鸟工具', 'https://c.runoob.com', '23453', 'CN'];

    connection.query(addSql, addSqlParams, function (error, results, fields) {
        if (error) {
            console.log('[INSERT ERROR] - ', error.message);

            connection.end();
            return deferred.reject('[INSERT ERROR] - ', error.message);
        }

        console.dir(results);
        deferred.resolve('INSERT ID:' + results.affectedRows);
        connection.end();
    });

    return deferred.promise;
}

function mysqlUpdate(id) {
    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'admin123',
        database: 'test20180217'
    });

    console.log("id === " + id);

    var deferred = Q.defer();

    connection.on('error', function () {
        console.error('createConnection 请正确启动 mysql 服务器');
        return deferred.reject('createConnection 请正确启动 mysql 服务器');
    });

    connection.connect(function (err) {
        if (err) {
            console.error('error connecting: ' + err.stack);
            return deferred.reject('error connecting: ' + err.stack);
        }
        console.log('connected as id ' + connection.threadId);
    });

    var modSql = 'UPDATE websites SET name = ?,url = ? WHERE Id = ?';
    var modSqlParams = ['菜鸟移动站' + id, 'https://m.runoob.com', id];

    connection.query(modSql, modSqlParams, function (error, results, fields) {
        if (error) {
            console.log('[UPDATE ERROR] - ', error.message);

            connection.end();
            return deferred.reject('[UPDATE ERROR] - ', error.message);
        }

        console.log('UPDATE affectedRows ：' + results.affectedRows);

        deferred.resolve('UPDATE affectedRows：' + results.affectedRows);
        connection.end();
    });

    return deferred.promise;
}

router.get('/update/:id', function (req, res, next) {
    var id = req.params.id;
    mysqlUpdate(id).then(function (results) {
        "use strict";
        res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });//设置response编码为utf-8
        if (results)
            res.end(JSON.stringify(results));
    },
        function (error) {
            "use strict";
            res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });//设置response编码为utf-8
            res.write("出错啦！");
            res.write("<br>");

            res.end(error);
        })

});

router.get("/insert", function (req, res, next) {
    mysqlInsert().then(function (results) {
        "use strict";
        res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });//设置response编码为utf-8
        if (results)
            res.end(JSON.stringify(results));
    },
        function (error) {
            "use strict";
            res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });//设置response编码为utf-8
            res.write("出错啦！");
            res.write("<br>");

            res.end(error);
        })

});

router.get("/delete", function (req, res, next) {
    mysqlDelete().then(function (results) {
        "use strict";
        res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });//设置response编码为utf-8
        if (results)
            res.end(JSON.stringify(results));
    },
        function (error) {
            "use strict";
            res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });//设置response编码为utf-8
            res.write("出错啦！");
            res.write("<br>");

            res.end(error);
        })

});

router.get("/query", function (req, res, next) {
    mysqlQuery().then(function (results) {
        "use strict";
        res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });//设置response编码为utf-8
        if (results)
            res.end(JSON.stringify(results));
    },
        function (error) {
            "use strict";
            res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });//设置response编码为utf-8
            res.write("出错啦！");
            res.write("<br>");

            res.end(error);
        })

});

 */
router.get('/mysqlAsync', async (ctx, next) => {
    await ctx.render('mysqlAsync', {
        title: 'mysql 操作列表'
    }
    )
});


module.exports = router;
